﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using MenuLibrary;
using PointOfSale.PointOfSale;
using PointOfSale.Configuration.Category;
using PointOfSale.Configuration.Department;
using PointOfSale.Configuration.Unit;
using PointOfSale.Configuration.User;
using PointOfSale.Configuration.Vendor;
using PointOfSale.Configuration.Item;
using PointOfSale.Report.Cashier;
using PointOfSale.POSSystem;
using PointOfSale.Transaction;
using PointOfSale.Global;
using PointOfSale.Report.Stock;
using PointOfSale.Report.Vendor;
using PointOfSale.Report.Monthly;
using PointOfSale.Report.Purchase;
using PointOfSale.Report.Sale;
using PointOfSale.Report.Yearly;
using PointOfSale.Report.Category;
using PointOfSale.Report.POSList;
using PointOfSale.Report.Department;
using PointOfSale.Report.SaleList;
using System.Runtime.InteropServices;
using PointOfSale.Report.BaoCaoBanHangTheoNgay;
using PointOfSale.Report.BCHoadonban;
using PointOfSale.Report.BCNhapXuat;

namespace PointOfSale
{
	public class mainForm : System.Windows.Forms.Form
	{
		private System.ComponentModel.IContainer components;

		#region Fields
		private System.Windows.Forms.MainMenu mainMnu;
		private System.Windows.Forms.ImageList imgLst;
		private System.Windows.Forms.ToolBar toolBar1;
		private System.Windows.Forms.ToolBarButton toolBarButton1;
		private System.Windows.Forms.ToolBarButton toolBarButton2;
		private System.Windows.Forms.ToolBarButton toolBarButton3;
		private System.Windows.Forms.ToolBarButton toolBarButton4;
		private System.Windows.Forms.ToolBarButton toolBarButton5;
		private System.Windows.Forms.ToolBarButton toolBarButton6;
		private System.Windows.Forms.ToolBarButton toolBarButton7;
		private System.Windows.Forms.ToolBarButton toolBarButton8;
		private System.Windows.Forms.ToolBarButton toolBarButton9;
		private System.Windows.Forms.Label label1;
		public System.Windows.Forms.Label lblUserName;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.Label lblCompanyName;
		private System.Windows.Forms.ImageList imgListToolbar;
		
		delegate int MyWndProc(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
		delegate void MyWndProc1(ref Message m);
		delegate int HookProc(int code, IntPtr wparam, ref CWPSTRUCT cwp);

		[DllImport("User32.dll",CharSet = CharSet.Auto, SetLastError = true)]
		static extern IntPtr GetWindowDC(IntPtr handle);

		[DllImport("User32.dll",CharSet = CharSet.Auto, SetLastError = true)]
		static extern bool GetWindowRect(IntPtr handle, ref RECT r);

		[DllImport("User32.dll",CharSet = CharSet.Auto, SetLastError = true)]
		static extern IntPtr SetWindowLong(IntPtr hwnd, int index, MyWndProc my);

		[DllImport("User32.dll",CharSet = CharSet.Auto, SetLastError = true)]
		static extern IntPtr SetWindowLong(IntPtr hwnd, int index, int style);

		[DllImport("User32.dll",CharSet = CharSet.Auto, SetLastError = true)]
		static extern IntPtr GetWindowLong(IntPtr hwnd, int index);

		[DllImport("User32.dll",CharSet = CharSet.Auto, SetLastError = true)]
		static extern IntPtr ReleaseDC(IntPtr handle, IntPtr hDC);

		[DllImport("User32.dll",CharSet = CharSet.Auto, SetLastError = true)]
		static extern int CallWindowProc(IntPtr wndProc, IntPtr hwnd, 
			int msg, IntPtr wparam,	IntPtr lparam);

		[DllImport("User32.dll",CharSet = CharSet.Auto, SetLastError = true)]
		static extern IntPtr SetWindowsHookEx(int type, HookProc hook, IntPtr instance, int threadId);

		[DllImport("User32.dll",CharSet = CharSet.Auto, SetLastError = true)]
		static extern bool UnhookWindowsHookEx(IntPtr hookHandle);

		[DllImport("User32.dll",CharSet = CharSet.Auto, SetLastError = true)]
		static extern int GetWindowThreadProcessId(IntPtr hwnd, int Id);

		[DllImport("User32.dll",CharSet = CharSet.Auto, SetLastError = true)]
		static extern IntPtr GetMenu(IntPtr hwnd);

		[DllImport("User32.dll",CharSet = CharSet.Auto, SetLastError = true)]
		static extern int GetClassName(IntPtr hwnd, char[] className, int maxCount);

		[DllImport("User32.dll",CharSet = CharSet.Auto, SetLastError = true)]
		static extern int CallNextHookEx(IntPtr hookHandle, int code, IntPtr wparam, ref CWPSTRUCT cwp);

		[DllImport("User32.dll",CharSet = CharSet.Auto, SetLastError = true)]
		static extern int SendMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);

		[DllImport("Gdi32.dll",CharSet = CharSet.Auto, SetLastError = true)]
		static extern IntPtr CreateCompatibleDC(IntPtr hdc);

		[DllImport("User32.dll",CharSet = CharSet.Auto, SetLastError = true)]
		static extern IntPtr GetParent(IntPtr hwnd);

		[DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError = true)]
		static extern bool AnimateWindow(IntPtr hwnd, uint dwTime, uint dwFlags);

		[DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError = true)]
		static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndAfter, 
			int x, int y, int cx, int cy, uint flags);

		//The win32 rectangle structure
		[StructLayout(LayoutKind.Sequential)]
			public struct RECT
		{
			public int left;
			public int top;
			public int right;
			public int bottom;
		}

		//Needed for the hook method
		[StructLayout(LayoutKind.Sequential)]
			public struct CWPSTRUCT
		{
			public IntPtr lparam;
			public IntPtr wparam;
			public int message;
			public IntPtr hwnd;
		}
		//Needed for filling the NCCALCSIZE_PARAMS struct
		[StructLayout(LayoutKind.Sequential)]
			public struct WINDOWPOS
		{
			public IntPtr hwnd;
			public IntPtr hwndAfter;
			public int x;
			public int y;
			public int cx;
			public int cy;
			public uint flags;
		}

		[StructLayout(LayoutKind.Sequential)]
			public struct NCCALCSIZE_PARAMS
		{
			public RECT rgc;
			public WINDOWPOS wndpos;
		}

		IntPtr defaultWndProc = IntPtr.Zero;//Pointer to menu's default WndProc
		MyWndProc subWndProc;//Delegate of type MyWndProc - needed to subclass the window
		HookProc hookProc;//This is delegate of type HookProc - needed to process the hooked window
		IntPtr hookHandle = IntPtr.Zero;//Pointer to hookProc
		IntPtr topPopupMenu = IntPtr.Zero;
		IntPtr firstPopupMenu = IntPtr.Zero;
		int itemWidth = 0;
		bool underTopItem = false;
		bool topItemSelected = false;
		#endregion

		public mainForm()
		{
			InitializeComponent();
			#region Init menu
			//hookProc = new HookProc(Hooked);
			//Construct the delegate that will process the subclassed windows proc
			subWndProc = new MyWndProc(SubclassWndProc);
			
			InitMenuItems();

			if(Menu != null)
			{
				EventHandler e = new EventHandler(SelectedMenuItem);
				foreach(MenuItem mi in Menu.MenuItems)
				{
					AddSelectHandler(mi, e);					
					mi.Select += e;
				}
			}
			
			//Hook all the messages send through this form
			hookHandle = SetWindowsHookEx(4, hookProc, IntPtr.Zero ,GetWindowThreadProcessId(Handle,0));
			#endregion
		}

		#region Some needed methods.
		#region Subclassed wndProc

//		int Hooked(int code, IntPtr wparam, ref CWPSTRUCT cwp)
//		{
//			switch(code)
//			{
//				case 0://HC_ACTION -> this means that the hook procedure should process the message
//					//contained in CWPSTRUCT
//				switch(cwp.message)
//				{
//					case 0x0001://WM_CREATE - catch this before the window is created
//						string s = string.Empty;
//						char[] className = new char[10];
//						//Get the window class name
//						int length = GetClassName(cwp.hwnd,className,9);
//						//Convert it to string
//						for(int i=0;i<length;i++)
//							s += className[i];
//						//Now check if the window is a menu
//						if(s == "#32768")//System class for menu
//							//if true - subclass the window
//							defaultWndProc = SetWindowLong(cwp.hwnd, (-4), subWndProc);
//						break;
//				}
//					break;
//			}
//			return CallNextHookEx(hookHandle,code,wparam, ref cwp);
//		}

		int SubclassWndProc(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam)
		{
			//tracer.Items.Add(msg.ToString().Trim());
			switch(msg)
			{
					/*case 0x0001://WM_CREATE					
						break;
					case 0x0018://WM_SHOWWINDOW
					
						return 0;*/
				case 0x0085://WM_NCPAINT
					IntPtr menuDC  = GetWindowDC(hwnd);					
					Graphics g = Graphics.FromHdc(menuDC);

					DrawBorder(g,underTopItem);
                    
					ReleaseDC(hwnd,menuDC);
					g.Dispose();
					return 0;
					//case 0x0046://WM_WINDOWPOSCHANGING
					//case 0x0047://WM_WINDOWPOSCHANGED
					//WINDOWPOS wpos = new WINDOWPOS();
					//wpos = (WINDOWPOS)Marshal.PtrToStructure(lparam,typeof(WINDOWPOS));
					//SetWindowPos(hwnd,IntPtr.Zero,wpos.x+2,wpos.y+2,wpos.cx,wpos.cy,0x0004);
					//SendMessage(hwnd,0x0317,IntPtr.Zero,lparam);
					//return 0;
				case 0x0083://WM_NCCALCSIZE
					/*//Get the NCCALCSIZE_PARAMS from the lparam pointer
					NCCALCSIZE_PARAMS calc = (NCCALCSIZE_PARAMS)Marshal.PtrToStructure(lparam,typeof(NCCALCSIZE_PARAMS));
					//Now adjust the borders of the menu window
					calc.rgc.top += 5;
					calc.rgc.right += 5;					
					calc.rgc.right -= 5;
					calc.rgc.bottom -= 5;
					//IntPtr newWparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(calc));
					//Marshal.StructureToPtr(calc,newWparam,true);
					CallWindowProc(defaultWndProc,hwnd,msg,wparam,lparam);
					return 0;*/
					break;
					/*case 0x0014://WM_ERASEBKGND -> This message uccurs before WM_NCPAINT
						//So send the message WM_NCPAINT
						g = Graphics.FromHdc(wparam);
						g.Clear(SystemColors.Menu);
						g.Dispose();
						SendMessage(hwnd, 0x0085, wparam, lparam);					
						break;*/
				case 0x0317://WM_PRINT
					int result = CallWindowProc(defaultWndProc,hwnd,msg,wparam,lparam);
					menuDC  = wparam;
					g = Graphics.FromHdc(menuDC);
					//Draw the border around the menu
					DrawBorder(g,underTopItem);
                    
					ReleaseDC(hwnd,menuDC);
					g.Dispose();
					return result;
			}			
			return CallWindowProc(defaultWndProc,hwnd,msg,wparam,lparam);
		}

		#endregion

		#region MainForm WndProc

		protected override void WndProc(ref Message m)
		{
			switch(m.Msg)
			{
				case 0x0125://WM_UNINITMENUPOPUP
					//Set the underTopItem flag to false
					if(underTopItem)
						underTopItem = false;
					base.WndProc(ref m);
					break;
				case 0x0002://WM_DESTROY -> we have to unhook the hooked windows
					UnhookWindowsHookEx(hookHandle);					
					base.WndProc(ref m);
					break;
				default:
					base.WndProc(ref m);
					break;
			}
		}
		
		#endregion

		#region Event Handlers

		void SelectedMenuItem(object sender, EventArgs e)
		{
			if(sender is TopMenuItem)
			{
				itemWidth = ((TopMenuItem)sender).Width;
				underTopItem = true;
			}
			else
			{
				MenuItem m = sender as MenuItem;
				if(m.IsParent)
					underTopItem = false;
				else
				{
					if(m.Parent is TopMenuItem)
						underTopItem = true;
				
					else
						underTopItem = false;					
				}
			}
		}

		void TopMenuItemsPopup(object sender, EventArgs e)
		{			
		}

		#endregion

		#region Support methods

		void AddSelectHandler(MenuItem mi, EventHandler e)
		{
			foreach(MenuItem m in mi.MenuItems)
			{
				m.Select += e;
				if(m.IsParent)
					AddSelectHandler(m, e);
			}
		}

		void DrawBorder(Graphics g, bool underTop)
		{
			Rectangle r = new Rectangle(0,0,(int)g.VisibleClipBounds.Width-1, 
				(int)g.VisibleClipBounds.Height-1);					
			Rectangle r1 = new Rectangle(1,1,(int)g.VisibleClipBounds.Width-3, 
				(int)g.VisibleClipBounds.Height-3);
			Rectangle r2 = new Rectangle(2,2,(int)g.VisibleClipBounds.Width-5, 
				(int)g.VisibleClipBounds.Height-5);

			if(underTop)
			{
				g.DrawLine(Pens.Black,r.Left,r.Bottom,r.Left,r.Top);
				g.DrawLine(new Pen(SystemColors.Menu),r.Left+1,r.Top,
					r.Left+itemWidth-2,r.Top);
				g.DrawLine(Pens.Black,r.Left+itemWidth-1,r.Top,r.Right,r.Top);
				g.DrawLine(Pens.Black,r.Right,r.Top,r.Right,r.Bottom);
				g.DrawLine(Pens.Black,r.Left,r.Bottom,r.Right,r.Bottom);
			}
			else
				g.DrawRectangle(Pens.Black,r);

			g.DrawRectangle(new Pen(SystemColors.Menu),r1);
			g.DrawRectangle(new Pen(SystemColors.Menu),r2);
		}
		#endregion

		#region Menu declaire.
		void InitMenuItems()
		{
			TopMenuItem tmiSystem = new TopMenuItem(clsGlobal.g_Hashtable["tmiSystem"].ToString());
			TopMenuItem tmiConfig = new TopMenuItem(clsGlobal.g_Hashtable["tmiConfig"].ToString());
			TopMenuItem tmiPoOfSa = new TopMenuItem(clsGlobal.g_Hashtable["tmiPoOfSa"].ToString());
			TopMenuItem tmiTrans = new TopMenuItem(clsGlobal.g_Hashtable["tmiTrans"].ToString());
			TopMenuItem tmiReport = new TopMenuItem(clsGlobal.g_Hashtable["tmiReport"].ToString());
			TopMenuItem tmiHelp = new TopMenuItem(clsGlobal.g_Hashtable["tmiHelp"].ToString());

			Menu.MenuItems.AddRange(new MenuItem[]{tmiSystem, tmiConfig, tmiPoOfSa, tmiTrans, tmiReport, tmiHelp});
			
			#region System.
			MyMenuItem miParameters = new MyMenuItem(clsGlobal.g_Hashtable["miParameters"].ToString());
			//miParameters.ItemImage = imgLst.Images[0];
			miParameters.Click += new System.EventHandler(this.miParameters_Click);

			MyMenuItem miSeparate100 = new MyMenuItem("-");

			MyMenuItem miChangeUser = new MyMenuItem(clsGlobal.g_Hashtable["miChangeUser"].ToString());
			miChangeUser.ItemImage = imgLst.Images[0];
			miChangeUser.Click += new System.EventHandler(this.miChangeUser_Click);

			MyMenuItem miChangePass = new MyMenuItem(clsGlobal.g_Hashtable["miChangePass"].ToString());
			miChangePass.ItemImage = imgLst.Images[0];
			miChangePass.Click += new System.EventHandler(this.miChangePass_Click);

			MyMenuItem miSeparate0 = new MyMenuItem("-");

			MyMenuItem miExit = new MyMenuItem(clsGlobal.g_Hashtable["miExit"].ToString());			
			miExit.ItemImage = imgLst.Images[1];			
			miExit.Click += new System.EventHandler(this.miExit_Click);

			Menu.MenuItems[0].MenuItems.AddRange(new MenuItem[]{miParameters, miSeparate100, miChangeUser, miChangePass, miSeparate0, miExit});
			#endregion

			#region Configuration
			MyMenuItem miDepartment = new MyMenuItem(clsGlobal.g_Hashtable["miDepartment"].ToString());
			miDepartment.ItemImage = imgLst.Images[4];	
			miDepartment.Click += new System.EventHandler(this.miDepartment_Click);

			MyMenuItem miCategory = new MyMenuItem(clsGlobal.g_Hashtable["miCategory"].ToString());
			miCategory.ItemImage = imgLst.Images[5];	
			miCategory.Click += new System.EventHandler(this.miCategory_Click);

			MyMenuItem miUnit = new MyMenuItem(clsGlobal.g_Hashtable["miUnit"].ToString());
			miUnit.ItemImage = imgLst.Images[6];	
			miUnit.Click += new System.EventHandler(this.miUnit_Click);

			MyMenuItem miItem = new MyMenuItem(clsGlobal.g_Hashtable["miItem"].ToString());
			miItem.ItemImage = imgLst.Images[8];	

			MyMenuItem miVendor = new MyMenuItem(clsGlobal.g_Hashtable["miVendor"].ToString());
			miVendor.ItemImage = imgLst.Images[9];	
			miVendor.Click += new System.EventHandler(this.miVendor_Click);

			MyMenuItem miUser = new MyMenuItem(clsGlobal.g_Hashtable["miUser"].ToString());
			miUser.ItemImage = imgLst.Images[10];				
			miUser.Click += new System.EventHandler(this.miUser_Click);

			MyMenuItem miSeparate1 = new MyMenuItem("-");
			MyMenuItem miSeparate2 = new MyMenuItem("-");
			MyMenuItem miSeparate3 = new MyMenuItem("-");

			Menu.MenuItems[1].MenuItems.AddRange(new MenuItem[]{miDepartment, miCategory, miSeparate1, miUnit, miSeparate2, miItem, miSeparate3, miVendor, miUser});
			
			#endregion

			#region Point Of Sale.
			MyMenuItem miSale = new MyMenuItem(clsGlobal.g_Hashtable["miSale"].ToString());
			miSale.ItemImage = imgLst.Images[11];
			miSale.Click += new System.EventHandler(this.miSale_Click);
			miSale.Shortcut = System.Windows.Forms.Shortcut.F1;

			MyMenuItem miPurchase = new MyMenuItem(clsGlobal.g_Hashtable["miPurchase"].ToString());
			miPurchase.ItemImage = imgLst.Images[12];
			miPurchase.Click += new System.EventHandler(this.miPurchase_Click);

			MyMenuItem miRuouPhaDacBiet = new MyMenuItem("Rượu pha lạ");
			miRuouPhaDacBiet.ItemImage = imgLst.Images[11];
			miRuouPhaDacBiet.Click += new System.EventHandler(this.miRuouPhaLa_Click);

			Menu.MenuItems[2].MenuItems.AddRange(new MenuItem[]{miSale, miPurchase, miRuouPhaDacBiet});
			#endregion

			#region Transaction.
			MyMenuItem miSaleTrans = new MyMenuItem(clsGlobal.g_Hashtable["miSaleTrans"].ToString());
			miSaleTrans.ItemImage = imgLst.Images[13];
			miSaleTrans.Click += new System.EventHandler(this.miSaleTrans_Click);

			MyMenuItem miPurchaseTrans = new MyMenuItem(clsGlobal.g_Hashtable["miPurchaseTrans"].ToString());
			miPurchaseTrans.ItemImage = imgLst.Images[14];
			miPurchaseTrans.Click += new System.EventHandler(this.miPurchaseTrans_Click);

			Menu.MenuItems[3].MenuItems.AddRange(new MenuItem[]{miSaleTrans, miPurchaseTrans});
			#endregion

			#region Reports.
			MyMenuItem miStock = new MyMenuItem("Hàng trong kho");
			miStock.ItemImage = imgLst.Images[15];
			miStock.Click += new System.EventHandler(this.miDepWiseStockReport_Click);

			MyMenuItem miBanHangTheoNgay = new MyMenuItem("Bán hàng hàng ngày");
			miBanHangTheoNgay.ItemImage = imgLst.Images[15];
			miBanHangTheoNgay.Click += new System.EventHandler(this.miBangHangTheoNgay_Click);

			MyMenuItem miPostingReport = new MyMenuItem("Báo cáo phiếu ký");
			miPostingReport.ItemImage = imgLst.Images[15];
			miPostingReport.Click += new System.EventHandler(this.miPostingReport_Click);

			MyMenuItem miHDBan = new MyMenuItem("Hóa đơn bán hàng chi tiết");
			miHDBan.ItemImage = imgLst.Images[15];
			miHDBan.Click += new System.EventHandler(this.miHDBan_Click);

			MyMenuItem miHDNhap = new MyMenuItem("Hóa đơn Nhập hàng chi tiết");
			miHDNhap.ItemImage = imgLst.Images[15];
			miHDNhap.Click += new System.EventHandler(this.miHDNhap_Click);

			MyMenuItem miXuatnoibo = new MyMenuItem("Báo cáo xuất nội bộ");
			miXuatnoibo.ItemImage = imgLst.Images[15];
			miXuatnoibo.Click += new System.EventHandler(this.miXuatnoibo_Click);

			Menu.MenuItems[4].MenuItems.AddRange(new MenuItem[]{miStock, miBanHangTheoNgay,miPostingReport,miHDBan,miHDNhap,miXuatnoibo});
			#endregion

			#region Help.
			MyMenuItem miProduct = new MyMenuItem(clsGlobal.g_Hashtable["miProduct"].ToString());
			miProduct.ItemImage = imgLst.Images[23];
			miProduct.Click += new System.EventHandler(this.miProduct_Click);

			MyMenuItem miAbout = new MyMenuItem(clsGlobal.g_Hashtable["miAbout"].ToString());
			miAbout.ItemImage = imgLst.Images[24];
			miAbout.Click += new System.EventHandler(this.miAbout_Click);

			Menu.MenuItems[5].MenuItems.AddRange(new MenuItem[]{miProduct, miAbout});
			#endregion

			#region Item.
			MyMenuItem miAddItem = new MyMenuItem(clsGlobal.g_Hashtable["miAddItem"].ToString());
			miAddItem.Click += new System.EventHandler(this.miAddItem_Click);

			MyMenuItem miItemDepartment = new MyMenuItem(clsGlobal.g_Hashtable["miItemDepartment"].ToString());			
			miItemDepartment.Click += new System.EventHandler(this.miItemDepartment_Click);

			MyMenuItem miSetupStaple = new MyMenuItem(clsGlobal.g_Hashtable["miSetupStaple"].ToString());			
			miSetupStaple.Click += new System.EventHandler(this.miSetupStaple_Click);

			MyMenuItem miSetupMenu = new MyMenuItem("Thiết lập thực đơn");			
			miSetupMenu.Click += new System.EventHandler(this.miSetupMenu_Click);

			miItem.MenuItems.AddRange(new MenuItem[]{miAddItem, miItemDepartment, miSetupStaple, miSetupMenu});

			#endregion
		}

		#endregion
		#endregion
		protected override void Dispose( bool disposing )
		{
			if( disposing)
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(mainForm));
			this.mainMnu = new System.Windows.Forms.MainMenu();
			this.imgLst = new System.Windows.Forms.ImageList(this.components);
			this.imgListToolbar = new System.Windows.Forms.ImageList(this.components);
			this.toolBar1 = new System.Windows.Forms.ToolBar();
			this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton4 = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton5 = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton6 = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton7 = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton8 = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton9 = new System.Windows.Forms.ToolBarButton();
			this.label1 = new System.Windows.Forms.Label();
			this.lblUserName = new System.Windows.Forms.Label();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.lblCompanyName = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// imgLst
			// 
			this.imgLst.ImageSize = new System.Drawing.Size(16, 16);
			this.imgLst.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imgLst.ImageStream")));
			this.imgLst.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// imgListToolbar
			// 
			this.imgListToolbar.ImageSize = new System.Drawing.Size(75, 50);
			this.imgListToolbar.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imgListToolbar.ImageStream")));
			this.imgListToolbar.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// toolBar1
			// 
			this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
																						this.toolBarButton1,
																						this.toolBarButton2,
																						this.toolBarButton3,
																						this.toolBarButton4,
																						this.toolBarButton5,
																						this.toolBarButton6,
																						this.toolBarButton7,
																						this.toolBarButton8,
																						this.toolBarButton9});
			this.toolBar1.DropDownArrows = true;
			this.toolBar1.ImageList = this.imgListToolbar;
			this.toolBar1.Location = new System.Drawing.Point(0, 0);
			this.toolBar1.Name = "toolBar1";
			this.toolBar1.ShowToolTips = true;
			this.toolBar1.Size = new System.Drawing.Size(792, 62);
			this.toolBar1.TabIndex = 1;
			this.toolBar1.TextAlign = System.Windows.Forms.ToolBarTextAlign.Right;
			this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
			// 
			// toolBarButton1
			// 
			this.toolBarButton1.ImageIndex = 0;
			this.toolBarButton1.ToolTipText = "Change user";
			// 
			// toolBarButton2
			// 
			this.toolBarButton2.ImageIndex = 1;
			this.toolBarButton2.ToolTipText = "Sale";
			// 
			// toolBarButton3
			// 
			this.toolBarButton3.ImageIndex = 2;
			this.toolBarButton3.ToolTipText = "Purchase";
			// 
			// toolBarButton4
			// 
			this.toolBarButton4.ImageIndex = 3;
			this.toolBarButton4.ToolTipText = "Manage items";
			// 
			// toolBarButton5
			// 
			this.toolBarButton5.ImageIndex = 4;
			this.toolBarButton5.ToolTipText = "Sale transaction";
			// 
			// toolBarButton6
			// 
			this.toolBarButton6.ImageIndex = 5;
			this.toolBarButton6.ToolTipText = "Purcharse Transaction";
			// 
			// toolBarButton7
			// 
			this.toolBarButton7.ImageIndex = 6;
			this.toolBarButton7.ToolTipText = "Sale report";
			// 
			// toolBarButton8
			// 
			this.toolBarButton8.ImageIndex = 7;
			this.toolBarButton8.ToolTipText = "Purchase report";
			// 
			// toolBarButton9
			// 
			this.toolBarButton9.ImageIndex = 8;
			this.toolBarButton9.ToolTipText = "Exit to window";
			// 
			// label1
			// 
			this.label1.BackColor = System.Drawing.Color.CornflowerBlue;
			this.label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.label1.Dock = System.Windows.Forms.DockStyle.Top;
			this.label1.Location = new System.Drawing.Point(0, 62);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(792, 34);
			this.label1.TabIndex = 2;
			// 
			// lblUserName
			// 
			this.lblUserName.Location = new System.Drawing.Point(0, 0);
			this.lblUserName.Name = "lblUserName";
			this.lblUserName.Size = new System.Drawing.Size(100, 8);
			this.lblUserName.TabIndex = 6;
			// 
			// pictureBox1
			// 
			this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
			this.pictureBox1.Location = new System.Drawing.Point(0, 96);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(792, 461);
			this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
			this.pictureBox1.TabIndex = 5;
			this.pictureBox1.TabStop = false;
			// 
			// lblCompanyName
			// 
			this.lblCompanyName.Location = new System.Drawing.Point(752, 8);
			this.lblCompanyName.Name = "lblCompanyName";
			this.lblCompanyName.Size = new System.Drawing.Size(24, 8);
			this.lblCompanyName.TabIndex = 7;
			// 
			// mainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.SystemColors.Control;
			this.ClientSize = new System.Drawing.Size(792, 557);
			this.ControlBox = false;
			this.Controls.Add(this.lblCompanyName);
			this.Controls.Add(this.pictureBox1);
			this.Controls.Add(this.lblUserName);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.toolBar1);
			this.Menu = this.mainMnu;
			this.Name = "mainForm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Quản lý Restaurant";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.Closing += new System.ComponentModel.CancelEventHandler(this.mainForm_Closing);
			this.Load += new System.EventHandler(this.mainForm_Load);
			this.Paint += new System.Windows.Forms.PaintEventHandler(this.mainForm_Paint);
			this.ResumeLayout(false);

		}
		#endregion

		[STAThread]
		static void Main()
		{
			Application.EnableVisualStyles();
			Application.Run(new frmFace());
			Application.Run(new frmPOSLogin());
			Application.Run(new mainForm());
		}
		
		#region Menu Click Event.
		#region System.
		private void miParameters_Click(object sender, System.EventArgs e)
		{
			frmParameters _frmParameters = new frmParameters();
			_frmParameters.ShowDialog();
		}
		private void miChangeUser_Click(object sender, System.EventArgs e)
		{
			clsGlobal.g_isChangeUser = true;
			frmCashiersReport _frmCashiersReport = new frmCashiersReport();
			_frmCashiersReport.ShowDialog();
			clsGlobal.g_UserId = 0;
			clsGlobal.g_UserName ="";
			clsGlobal.g_DepartmentId = 0;
			lblUserName.Text = "";
			frmPOSLogin _frmPOSLogin = new frmPOSLogin();
			_frmPOSLogin.ShowDialog();
		}

		private void miChangePass_Click(object sender, System.EventArgs e)
		{
			frmChangePassword _frmChangePassword = new frmChangePassword();
			_frmChangePassword.Show();
		}

		private void miExit_Click(object sender, System.EventArgs e)
		{
			clsGlobal.g_isExit = true;
			frmCashiersReport _frmCashiersReport = new frmCashiersReport();
			_frmCashiersReport.ShowDialog();
		}
		#endregion

		#region Configuration
		private void miDepartment_Click(object sender, System.EventArgs e)
		{
			frmDepartmentMaster _frmDepartment = new frmDepartmentMaster();
			_frmDepartment.Show();
		}

		private void miCategory_Click(object sender, System.EventArgs e)
		{
			frmItemCategory _frmItemCategory = new frmItemCategory();
			_frmItemCategory.Show();
		}

		private void miUnit_Click(object sender, System.EventArgs e)
		{
			frmUnit _frmUnit = new frmUnit();
			_frmUnit.Show();
		}

		private void miVendor_Click(object sender, System.EventArgs e)
		{
			frmVendorDataBase _frmVendorDataBase = new frmVendorDataBase();
			_frmVendorDataBase.Show();
		}

		private void miUser_Click(object sender, System.EventArgs e)
		{
			frmPOSUsers _frmPOSUsers = new frmPOSUsers();
			_frmPOSUsers.Show();
		}
		#endregion

		#region Point Of Sale
		private void miSale_Click(object sender, System.EventArgs e)
		{
			frmPOSTransaction _frmPOSTransaction = new frmPOSTransaction();
			_frmPOSTransaction.Show();
		}

		private void miPurchase_Click(object sender, System.EventArgs e)
		{
			frmPOSPurchaseBill _frmPOSPurchaseBill = new frmPOSPurchaseBill();
			_frmPOSPurchaseBill.Show();
		}

		private void miRuouPhaLa_Click(object sender, System.EventArgs e)
		{
			frmRuouPhaDacBiet frm = new frmRuouPhaDacBiet();
			frm.Show();
		}
		#endregion

		#region Transaction.
		private void miSaleTrans_Click(object sender, System.EventArgs e)
		{
			frmPOSTransactionsList _frmPOSTransactionsList = new frmPOSTransactionsList();
			_frmPOSTransactionsList.Show();
		}

		private void miPurchaseTrans_Click(object sender, System.EventArgs e)
		{
			frmPOSPurchaseBillList _frmPOSPurchaseBillList = new frmPOSPurchaseBillList();
			_frmPOSPurchaseBillList.Show();
		}
		#endregion

		#region Report.
		private void miVendorRe_Click(object sender, System.EventArgs e)
		{
			frmVendorSumaryReport _frmVendorSumaryReport = new frmVendorSumaryReport();
			_frmVendorSumaryReport.Show();
		}

		private void miCategoryRe_Click(object sender, System.EventArgs e)
		{
			frmCategoryReport _frmCategoryReport = new frmCategoryReport();
			_frmCategoryReport.Show();
		}

		private void miDepartmentRe_Click(object sender, System.EventArgs e)
		{
		}
		#endregion

		#region Help.
		private void miProduct_Click(object sender, System.EventArgs e)
		{
		}
		private void miAbout_Click(object sender, System.EventArgs e)
		{
		}
		#endregion

		#region Item.
		private void miAddItem_Click(object sender, System.EventArgs e)
		{
			frmItemDefinition _frmItemDefinition = new frmItemDefinition();
			_frmItemDefinition.Show();
		}

		private void miItemDepartment_Click(object sender, System.EventArgs e)
		{
			frmItemDepartment _frmItemDepartment = new frmItemDepartment();
			_frmItemDepartment.Show();
		}

		private void miSetupStaple_Click(object sender, System.EventArgs e)
		{
			frmSetupStaple _frmSetupStaple = new frmSetupStaple();
			_frmSetupStaple.Show();
		}

		private void miSetupMenu_Click(object sender, System.EventArgs e)
		{
			frmSetupMenu _frmSetupMenu = new frmSetupMenu();
			_frmSetupMenu.Show();
		}
		#endregion

		#region Stock report
		private void miDepWiseStockReport_Click(object sender, System.EventArgs e)
		{
			frmDepartmentWiseStock frm = new frmDepartmentWiseStock(2);
			frm.Show();
		}
		private void miBangHangTheoNgay_Click(object sender, System.EventArgs e)
		{
			frmBCBanhang frm = new frmBCBanhang();
			frm.Show();
		}
		private void miPostingReport_Click(object sender, System.EventArgs e)
		{
			frmPOSListReport frm = new frmPOSListReport();
			frm.Show();
		}

		private void miHDBan_Click(object sender, System.EventArgs e)
		{
			frmHDBan frm = new frmHDBan();
			frm.Show();
		}
		private void miHDNhap_Click(object sender, System.EventArgs e)
		{
			frmHDNhap frm = new frmHDNhap();
			frm.Show();
		}
		private void miXuatnoibo_Click(object sender, System.EventArgs e)
		{
		 frmXuatNoiBo frm = new frmXuatNoiBo();
			frm.Show();
		}


		private void miOverStockReport_Click(object sender, System.EventArgs e)
		{
			frmReportOverStock _frmReportOverStock = new frmReportOverStock();
			_frmReportOverStock.Show();
		}
		private void miReOrderRe_Click(object sender, System.EventArgs e)
		{
			frmDepartmentWiseReOrderReport _frmDepartmentWiseReOrderReport = new frmDepartmentWiseReOrderReport();
			_frmDepartmentWiseReOrderReport.Show();
		}
		#endregion

		#region Monthly
		private void miMonSumRe_Click(object sender, System.EventArgs e)
		{
			frmMonthlyReport _frmMonthlyReport = new frmMonthlyReport();
			_frmMonthlyReport.Show();
		}
		private void miMonCoChart_Click(object sender, System.EventArgs e)
		{
		}
		#endregion

		#region Yearly
		private void miYearSumRe_Click(object sender, System.EventArgs e)
		{
			frmYearlyReport _frmYearlyReport = new frmYearlyReport();
			_frmYearlyReport.Show();
		}
		private void miYearCoChart_Click(object sender, System.EventArgs e)
		{
		}
		#endregion

		#region Purchase
		private void miPurchaseItemDetailReport_Click(object sender, System.EventArgs e)
		{
			frmPOSPurchaseItemDetailsReport _frmPOSPurchaseItemDetailsReport = new frmPOSPurchaseItemDetailsReport();
			_frmPOSPurchaseItemDetailsReport.Show();
		}

		private void miPurchaseListReport_Click(object sender, System.EventArgs e)
		{
			frmPOSPurchaseListReport _frmPOSPurchaseListReport = new frmPOSPurchaseListReport();
			_frmPOSPurchaseListReport.Show();
		}
		#endregion

		#endregion

		#region ToolBar click.
		private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			if(e.Button == this.toolBar1.Buttons[0])
			{
				clsGlobal.g_isChangeUser = true;
				frmCashiersReport _frmCashiersReport = new frmCashiersReport();
				_frmCashiersReport.ShowDialog();
				clsGlobal.g_UserId = 0;
				clsGlobal.g_UserName ="";
				clsGlobal.g_DepartmentId = 0;
				lblUserName.Text = "";
				frmPOSLogin _frmPOSLogin = new frmPOSLogin();
				_frmPOSLogin.ShowDialog();
			}

			if(e.Button == this.toolBar1.Buttons[1])
			{
				frmPOSTransaction _frmPOSTransaction = new frmPOSTransaction();
				_frmPOSTransaction.Show();
			}

			if(e.Button == this.toolBar1.Buttons[2])
			{
				frmPOSPurchaseBill _frmPOSPurchaseBill = new frmPOSPurchaseBill();
				_frmPOSPurchaseBill.Show();
			}

			if(e.Button == this.toolBar1.Buttons[3])
			{
				frmItemDefinition _frmItemDefinition = new frmItemDefinition();
				_frmItemDefinition.Show();
			}

			if(e.Button == this.toolBar1.Buttons[4])
			{
				frmPOSTransactionsList _frmPOSTransactionsList = new frmPOSTransactionsList();
				_frmPOSTransactionsList.Show();
			}

			if(e.Button == this.toolBar1.Buttons[5])
			{
				frmPOSPurchaseBillList _frmPOSPurchaseBillList = new frmPOSPurchaseBillList();
				_frmPOSPurchaseBillList.Show();
			}

			if(e.Button == this.toolBar1.Buttons[6])
			{
				frmPOSPurchaseItemDetailsReport _frmPOSPurchaseItemDetailsReport = new frmPOSPurchaseItemDetailsReport();
				_frmPOSPurchaseItemDetailsReport.Show();
			}

			if(e.Button == this.toolBar1.Buttons[7])
			{
				frmSaleList _frmSaleList = new frmSaleList();
				_frmSaleList.Show();
			}

			if(e.Button == this.toolBar1.Buttons[8])
			{
				clsGlobal.g_isExit = true;
//				frmCashiersReport _frmCashiersReport = new frmCashiersReport();
//				_frmCashiersReport.ShowDialog();
							
					Application.Exit();
					Environment.Exit(0);
				
			}
		}
		#endregion

		private void mainForm_Load(object sender, System.EventArgs e)
		{
			lblUserName.Text = clsGlobal.g_UserName;
			lblCompanyName.Text = clsGlobal.g_Hashtable["CompanyName"].ToString();
			// lblUserName
			// 
			this.lblUserName.BackColor = System.Drawing.Color.CornflowerBlue;
			this.lblUserName.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lblUserName.ForeColor = System.Drawing.SystemColors.ControlLightLight;
			this.lblUserName.Location = new System.Drawing.Point(label1.Location.X + label1.Size.Width - 310, 69);
			this.lblUserName.Name = "lblUserName";
			this.lblUserName.Size = new System.Drawing.Size(300, 22);
			this.lblUserName.TabIndex = 4;
			this.lblUserName.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// lblCompanyName
			// 
			this.lblCompanyName.BackColor = System.Drawing.Color.CornflowerBlue;
			this.lblCompanyName.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lblCompanyName.ForeColor = System.Drawing.SystemColors.ControlLightLight;
			this.lblCompanyName.Location = new System.Drawing.Point(label1.Location.X + 5, 69);
			this.lblCompanyName.Name = "lblCompanyName";
			this.lblCompanyName.Size = new System.Drawing.Size(352, 22);
			this.lblCompanyName.TabIndex = 6;
			this.lblCompanyName.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
		}

		private void mainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			clsGlobal.g_isExit = true;
			frmCashiersReport _frmCashiersReport = new frmCashiersReport();
			_frmCashiersReport.Show();
			this.Close();
		}
		#region Create delegate.
		//Khai bao con tro ham.
		public delegate  void Call(String userName);

		//Ham ma no thuc su tro den.
		public static void CallMethod(String userName)
		{
			
		}
		#endregion

		private void mainForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
			if(clsGlobal.g_UserFullName != "")
			{
				lblUserName.Text = clsGlobal.g_UserFullName;
			}
			else
			{
				lblUserName.Text = clsGlobal.g_UserName;
			}
			if(!clsGlobal.g_UserPrivilige)
			{
				Menu.MenuItems[1].MenuItems[0].Enabled = false;
				Menu.MenuItems[1].MenuItems[8].Enabled = false;
			}
			else
			{
				Menu.MenuItems[1].MenuItems[0].Enabled = true;
				Menu.MenuItems[1].MenuItems[8].Enabled = true;
			}
		}
	}
}
